local windowList = findWindowList("*","TWNClientFramework");
if( #windowList == 0 ) then
	print("You need to run Rift first!");
	return 0;
end

function getWin()
	if( __WIN == nil ) then
  		__WIN = windowList[1]
	end

	return __WIN;
end	

function getProc()
	if( __PROC == nil or not windowValid(__WIN) ) then
		if( __PROC ) then closeProcess(__PROC) end;
		__PROC = openProcess( findProcessByWindow(getWin()) );
	end

	return __PROC;
end

function memoryReadRepeat(_type, proc, address, offset)
	local readfunc;
	local ptr = false;
	local val;

	if( type(proc) ~= "userdata" ) then
		error("Invalid proc", 2);
	end

	if( type(address) ~= "number" ) then
		error("Invalid address", 2);
	end

	if( _type == "int" ) then
		readfunc = memoryReadInt;
	elseif( _type == "uint" ) then
		readfunc = memoryReadUInt;
	elseif( _type == "float" ) then
		readfunc = memoryReadFloat;
	elseif( _type == "byte" ) then
		readfunc = memoryReadByte;
	elseif( _type == "string" ) then
		readfunc = memoryReadString;
	elseif( _type == "ustring" ) then
		readfunc = memoryReadUString;
	elseif( _type == "intptr" ) then
		readfunc = memoryReadIntPtr;
		ptr = true;
	elseif( _type == "uintptr" ) then
		readfunc = memoryReadUIntPtr;
		ptr = true;
	elseif( _type == "byteptr" ) then
		readfunc = memoryReadBytePtr;
		ptr = true;
	elseif( _type == "floatptr" ) then
		readfunc = memoryReadFloatPtr;
		ptr = true;		
	else
		return nil;
	end

	for i = 1, 10 do
		showWarnings(false);
		if( ptr ) then
			val = readfunc(proc, address, offset);
		else
			val = readfunc(proc, address);
		end
		showWarnings(true);

		if( val ) then i = 11; end; -- Get out of loop
	end

	if( val == nil ) then
		local info = debug.getinfo(2);
		local name;
		if( info.name ) then
			name = info.name .. '()';
		else
			name = '<unknown>';
		end
--		logger:log('debug',  "Error reading memory in %s from %s:%d", name, info.short_src, info.currentline or 0);
--	no logger available at the time of using that
		local msg = sprintf("Error reading memory in %s from %s:%d", name, info.short_src, info.currentline or 0);
		error(msg, 2)

	end

	return val

end	
_Address = include("addresses.lua")
local mywin, proc, mainOffset
repeat 
   mywin = findProcess("RIFT*");
   proc = openProcess(mywin);
   mainOffset = getModuleAddress(mywin, "rift.exe");
until mainOffset ~= 0
printf("main offset 0x%X\n",mainOffset)
local offsets = {
{0xC,0x158,0x17C,0x344,0x54},
{0xC,0x168,0x17C,0x344,0x54},
{0x7C,0xE8,0x17C,0x344,0x54},
{0x8,0x138,0x17C,0x344,0x54},
{0x8,0x120,0x17C,0x344,0x54},
{0x10,0xA8,0x17C,0x344,0x54},
}
local foffset
--[[
showWarnings(false)
for k,v in pairs(offsets) do
	local fish = memoryReadIntPtr(getProc(),mainOffset+_Address.Fishbase,v)
	if fish and fish == 0 then foffset = v end
end
showWarnings(true)
printf("Currently using offsets of ")
for c,d in pairs(foffset) do
	printf("0x%X ",d)
end
printf("\n")
]]
function fish()
	return memoryReadInt(getProc(), mainOffset + 0x1edc7c44)
	--return memoryReadIntPtr(getProc(),mainOffset+_Address.Fishbase,foffset)
end

--attach(getWin())
setStartKey(key.VK_F5);
setStopKey(key.VK_F6);
local hf_x, hf_y, hf_wide, hf_high = windowRect( getWin())
printf("%d %d %d %d\n", hf_wide, hf_high, hf_x, hf_y );
print("5 seconds to get focus")
yrest(5000)
print("ok starting")
local starttime = os.time()
print(os.date())
function main()
	local count = 0
	while(true) do
		local resettime = os.time()
		keyboardPress(key.VK_1)
		yrest(1000)
		mouseLClick()
		repeat
			yrest(10)
			if os.difftime(os.time(), resettime) > 30 then break end
		until fish() == 3
			resettime = os.time()
		--print("ok fishing")
		repeat
			repeat
				yrest(10)
				if os.difftime(os.time(), resettime) > 30 then break end
			until fish() == 2
			--print("trying to catch now")
			keyboardPress(key.VK_1)
			repeat
				yrest(10)
				if os.difftime(os.time(), resettime) > 30 then break end
			until fish() ~= 2
			if os.difftime(os.time(), resettime) > 30 then break end
		until fish() == 0 or fish() == 4
		count = count + 1
		yrest(1000)		
		print("Caught "..count.." in "..os.difftime(os.time(), starttime).." seconds, casting again.")
	end
end
-- cast and then starts at 2
-- then changes to 3 means ur fishing
-- if 2 after being 3 means to click
-- if 4 means u got loot
-- if 0 means not fishing

startMacro(main,true);

--attach(getWin())
--[[keyboardHold(key.VK_A)
yrest(15000)
print("releasing")
keyboardRelease(key.VK_A)]]